package com.dream.myqiyi.search;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.py.app.NiActivity;

import com.cloud.httputil.HttpUtil;
import com.dream.myqiyi.R;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class NyzichanActivity extends Activity {

	TextView textView1 = null, textView2 = null, textView3 = null,
			textname1 = null;
	private static String TAG = "GUDING";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.zijin_view);
		
		ListView gdlistView = (ListView) findViewById(R.id.listview_gd);
		textView1 = (TextView) findViewById(R.id.numtext1);
		textView2 = (TextView) findViewById(R.id.nametext1);
		textView3 = (TextView) findViewById(R.id.unitnotext1);
		textname1 = (TextView) findViewById(R.id.nametext);

		textView1.setText("概要信息");
		textView2.setText("额度");
		textView3.setText("所属村组");
		System.out.println("===========现金银行存款============");

		HashMap<String, String> map = new HashMap<String, String>();
		map.put("sessionid", NiActivity.SESSIONID);
		map.put("unitno", NiActivity.UNITNO);
		map.put("selYear", "2013");
		map.put("listType", "7");
		// map.put("planYear", "2013");resourcelist
		String result = HttpUtil.get(
				"http://125.70.9.164:1026/app/android/fundsvoucherlist", map);

		final ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();

		try {
			JSONObject resultJObject = new JSONObject(result);
			JSONArray itemArray = resultJObject.getJSONArray("items");
			for (int i = 0; i < itemArray.length(); i++) {
				JSONObject assets = new JSONObject(itemArray.opt(i).toString());

//				String obtaindate = assets.getString("obtaindate");
//				System.out.println(obtaindate);
				
				String code = assets.getString("summary");
				String name = assets.getString("money");
				String unitname = assets.getString("unitname");
				String unitno = assets.getString("unitno");
				String sortword = assets.getString("sortword");
				String voucherno = assets.getString("voucherno");
				String voucherkey = assets.getString("voucherkey");
				String direction = assets.getString("direction");
//				String price = assets.getString("price");
//				String owner = assets.getString("owner");
//				String address = assets.getString("address");
				
				System.out.println(code + "==" + name + "==" + unitname );
//						+ "==" + type + "==" + amount + "==" + status);
//				
				HashMap<String, Object> listmap = new HashMap<String, Object>();
				listmap.put("numtext", code);
				listmap.put("name", name);
				listmap.put("unitnotext", unitname);
				listmap.put("unitno", unitno);
				listmap.put("sortword", sortword);
				listmap.put("voucherno", voucherno);
				listmap.put("voucherkey", voucherkey);
				listmap.put("direction", direction);
//				listmap.put("price", price);
//				listmap.put("owner", owner);
//				listmap.put("address", address);
				listItem.add(listmap);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			Log.i(TAG, e.toString());
			e.printStackTrace();
		}
		SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,
				R.layout.listlayout, 
				new String[] { "numtext", "name","unitnotext" }, 
				new int[] { R.id.numtext,R.id.nametext, R.id.unitnotext });

		gdlistView.setAdapter(listItemAdapter);

		gdlistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				String str1 = "概要信息：" + listItem.get(arg2).get("numtext") + "\n"
						+ "额度：" + listItem.get(arg2).get("name") + "\n"
						+
						"所属村组：" + listItem.get(arg2).get("unitnotext") +"\n"
						+ "帐套编号：" + listItem.get(arg2).get("unitno") +"\n"
						+ "类别：" + listItem.get(arg2).get("sortword") +"\n"
						+ "凭证号：" + listItem.get(arg2).get("voucherno") +"\n"
						+ "voucherkey：" + listItem.get(arg2).get("voucherkey") +"\n"
						+ "direction：" + listItem.get(arg2).get("direction") +"\n"
//						+ "单价：" + listItem.get(arg2).get("price") +"\n"
//						+ "责任人：" + listItem.get(arg2).get("owner") +"\n"
//						+ "存放地点：" + listItem.get(arg2).get("address") +"\n"
						;
				showJsonDialog(listItem.get(arg2).get("numtext").toString(), str1);
				System.out.println("=======click"+arg2+"======");
			}
		});
	}

	Dialog jsonDialog = null;
	TextView jsonTextView = null, jsonTitle = null;

	private void showJsonDialog(String title, String json) {
		if (jsonDialog == null) {
			jsonDialog = new Dialog(this);
			jsonDialog.setCancelable(true);
			jsonDialog.setCanceledOnTouchOutside(false);
			View v = View.inflate(this, R.layout.mydialog, null);
			jsonDialog.setContentView(v);
			jsonTextView = (TextView) v.findViewById(R.id.message);
			jsonTitle = (TextView) v.findViewById(R.id.title);
			v.findViewById(R.id.positiveButton).setOnClickListener(
					new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							if (jsonDialog != null) {
								jsonDialog.dismiss();
							}
						}
					});
		}
		jsonTextView.setText(json);
		jsonTitle.setText(title);
		jsonDialog.show();
	}
}
